;(function($) {
	//检测IE内核
	var browser={
		versions:function(){
			var u = navigator.userAgent, app = navigator.appVersion;
			return {
				trident: u.indexOf('Trident') > -1, //IE内核
				presto: u.indexOf('Presto') > -1, //opera内核
				webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
				gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
				mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
				ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
				iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
				iPad: u.indexOf('iPad') > -1, //是否iPad
				webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
			};
		}(),
		language:(navigator.browserLanguage || navigator.language).toLowerCase()
	}

	if(browser.versions.trident){
		conosle.log("ie");
		$("#login").css("background","#282828");
	}
	else if(browser.versions.mobile){
		
	}
	var isIE=!!window.ActiveXObject; 
	var isIE6=isIE&&!window.XMLHttpRequest; 
	var isIE8=isIE&&!!document.documentMode; 
	var isIE7=isIE&&!isIE6&&!isIE8; 
	if (isIE6 || isIE8|| isIE7){
		$("#login").css("background","#282828");
	} 
	if(isIE){
		alert(1);
	}

	$("body").css("overflow", "hidden");
	var width = document.body.clientWidth;
	var height = document.body.scrollHeight;

	$("#bodyBg").css({
		"width" : document.body.clientWidth,
		"height" : document.body.scrollHeight,
		"background" : "url(../images/loginbg.jpg)",
		"z-index" : "1",
		"background-size" : width * 1.5 + "px "
				+ height * 1.5 + "px",
		"background-position-x" : -(width * 1.5 - width) / 2,
		"background-position-y" : "-100px",
		"opacity" : "1",
	});
	$(".login").css("margin-top", 170 / 1080 * height);
	document.onreadystatechange = function() {
		if (document.readyState == "complete") {
			if (width / height > 1366 / 1000) {
				$("#bodyBg").animate({
					"background-size" : width,
					"background-position" : "0px",
					"background-position-y" : "0px"
				}, 3000);
			} else {
				var bgwidth = height / 1000 * 1366;
				$("#bodyBg").animate({
					"background-size" : bgwidth,
					"background-position-y" : "0px",
					"background-position-x" : "0px"
				}, 3000);
			}
		}
	};
	/* 更改input框 */
	$("input").css("color","#999");
	$("input").on("focus",function(){
		$("input").css("color","#999");
		$(this).css("color","#fff");
	});
	$("input").on("blur",function(){
		$("input").css("color","#999");
	});
	$("#loginMain").css({
		"width" : document.body.clientWidth,
		"height" : document.body.scrollHeight,
		"position" : "absolute",
		"top" : "0px",
		"left" : "0px",
		"z-index" : "2"
	});
	$("#loginMain").css({
		"background" : "rgb(0,0,0)",
		"opacity" : "0.7",
	});
	$(".errorMessage").bind("click", function() {
		$(".errorMessage").css("display", "none");
		$("input").css("opacity", "1");
		$(this).prev().select();
	});
	$(".forgetPassword").bind("click", function() {
		$("#login_password").parent().slideUp("slow");
		$(".login").animate({ height : 400 }, "slow");
		$("#user_login").fadeOut("slow", function() {
			$("#forgetPasswordButton").fadeIn("slow");
			$("#forgetPasswordButton").css("display", "block");
			$(".errorSign").html("");
		});
	});
	$("#signButton").bind("click", function() {
		// $(".login_form").hide();
		//刷新注册的验证码
		console.log($("#reg_vcode_img").attr("src"));
		if ($("#reg_vcode_img").attr("src") == '') {
			console.log('reg click');
			$("#reg_vcode_img").click();
		}
		$(".login_form").animate({ left : -100, opacity : 0 }, 300, "swing", function() {
			$(".errorSign").html("");
			$(".login_form").hide();
			$(".sign_form").css({
				"display" : "block"
			});
			$(".sign_form").animate({
				right : 0,
				opacity : 1
			}, 300);
		});
	});
	$("#loginButton").bind("click", function() {
		$(".sign_form").stop().animate({
			right : -100,
			opacity : 0
		}, 300, "swing", function() {
			$(".sign_form").hide();
			$(".login_form").css({ "display" : "block" });
			$(".login_form").stop().animate({
				left : 0,
				opacity : 1
			}, 300);
			$(".errorSign").html("");
		});
	});
	$("#loginButton, #signButton").bind(
		"click",
		function() {
			$(".forgetPassword").fadeIn("fast");
			$("#loginButton,#signButton").removeClass("active");
			$(this).addClass("active");
			$(".login").stop().animate({ height : 524 }, "slow");
			$("#password").parent().slideDown("slow");
//			$("#vcode").parent().slideDown("slow");
			$("#forgetPasswordButton").stop().fadeOut(
				"slow",
				function() {
					$("#btn_login").stop().fadeIn("slow");
					$("#btn_login").css("display", "block");
					$(".errorSign").text("");
				}
			);
	});
	$("#account, #password, #vcode, #sign_username, #sign_password, #sign_password2, #userCode").focus(function(){
		$(this).next().html("");
	});
	$("#account, #password, #vcode").focus(function(){
		$("#login_msg").html("");
	});
	$("#sign_username, #sign_password, #sign_password2, #userCode").focus(function(){
		$("#reg_msg").html("");
	});
	
	$("#account").keypress(function(event){
		if(event.keyCode == "13") {
			$("#password").focus();
		}
	});
	$("#password").keypress(function(event){
		if(event.keyCode == "13") {
			if ($("#vcode_div").is(":hidden")) {
				$("#btn_login").click();
			} else {
				$("#vcode").focus();
			}
		}
	});
	$("#vcode").keypress(function(event){
		if(event.keyCode == "13") {
			$("#btn_login").click();
		}
	});
	
	$("#btn_login").click(function() {
		var login_flag = $("#btn_login").attr("login");
		if (login_flag == 1) {
			return;
		}
		$("#login_msg").html("");
		var checkok = true;
		var account = $("#account");
		if (account.val() == "") {
			floatMsg.call(account, "请输入电子邮箱");
			account.animate({ "box-shadow" : "10px" }, 500);
			checkok = false;
		}
		var password = $("#password");
		if (password.val() == "") {
			floatMsg.call(password, "请输入密码");
			checkok = false;
		}
		var vcode = $("#vcode");
		if (vcode.val() == "") {
			$("#login_msg").html("请输入验证码");
			checkok = false;
		}
		if (!checkok) {
			return false;
		}
		post_data = {
			account	:  account.val(),
			password: password.val()
		};
		if (!$("#vcode_div").is(":hidden")) {
			post_data.vcode = vcode.val();
		}
		$.ajax({
			url : '/index/auth.do',
			dataType : "json",
			data : post_data,
			success : function(data) {
				if (data.msg) {
					$("#login_msg").html(data.msg);
					if (data.authCount && data.authCount >= 2) {
						$("#vcode_div").show();
						vcode.val("");
						$("#vcode_img").click();
					}
				} else {
					if (redirect_url != '') {
						window.location = redirect_url;
					} else {
						window.location = "/index/index.do";
					}
				}
			},
			all : function() {
				$("#btn_login").attr("login", 0);
			}
		});
		$("#btn_login").attr("login", 1);
	});
	
	$("#sign_username").keypress(function(event){
		if(event.keyCode == "13") {
			$("#sign_password").focus();
		}
	});
	$("#sign_password").keypress(function(event){
		if(event.keyCode == "13") {
			$("#sign_password2").focus();
		}
	});
	$("#sign_password2").keypress(function(event){
		if(event.keyCode == "13") {
			$("#reg_vcode").focus();
		}
	});
//	$("#userCode").keypress(function(event){
//		if(event.keyCode == "13") {
//			$("#btn_reg").click();
//		}
//	});
	$("#reg_vcode").keypress(function(event){
		if(event.keyCode == "13") {
			$("#btn_reg").click();
		}
	});

	$("#btn_reg").click(function() {
		if ($("#btn_reg").attr("reg") == 1) {
			return;
		}
		$("#reg_msg").html("");
		var checkok = true;
		var username = $("#sign_username");
		var rege_email = /^([a-zA-Z0-9_\.\-])+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,9}$/;
		if (username.val() == '') {
			floatMsg.call(username, "请输入注册邮箱");
			checkok = false;
		} else if (!rege_email.test(username.val())) {
			floatMsg.call(username, "注册邮箱不合法");
			checkok = false;
		}
		console.log(checkok);
		var password = $("#sign_password");
		var password2 = $("#sign_password2");
		if (password.val() == password2.val()) {
			var reg = /^.{6,18}$/;
			if (!reg.test(password.val())) {
				floatMsg.call(password, "密码为6到18位");
				checkok = false;
			}
			var reg = /[a-zA-Z0-9!@#\*_]/;
			if (!reg.test(password.val())) {
				floatMsg.call(password, "密码包含：字母、数字和!@#*_");
				floatMsg.call(password2, "密码包含：字母、数字和!@#*_");
				checkok = false;
			}
		} else {
			floatMsg.call(password, "两次输入密码不一致");
			floatMsg.call(password2, "两次输入密码不一致");
			checkok = false;
		}

		var regVcode = $("#reg_vcode");
		if (regVcode.val() == '') {
			$("#reg_msg").html("请输入验证码");
			checkok = false;
		}
		if (!checkok) {
			return false;
		}

		$.ajax({
			url : '/index/saveReg.do',
			dataType : "json",
			data : {
				userEmail	:  username.val(),
				userPssword: password.val(),
				regVcode	: regVcode.val()
			},
			success : function(data) {
				if (data.msg) {
					$("#reg_msg").html(data.msg);
					$("#reg_vcode_img").click();
					$("#reg_vcode").val('');
				} else {
					if (redirect_url != '') {
						window.location = redirect_url;
					} else {
						window.location = "/index/index.do";
					}
				}
			},
			all : function() {
				$("#btn_reg").attr("reg", 0);
			}
		});
		$("#btn_reg").attr("reg", 1);
	});
	
	if (reg_login == 'reg') {
		$("#signButton").click();
	}
	var codeNum = 1;
	$("#vcode_img").click(function() {
		$(this).attr('src', "/index/refreshVcode.do?codeNum=" + (codeNum++));
	});
	$("#reg_vcode_img").click(function() {
		$(this).attr('src', "/index/refreshRegVcode.do?codeNum=" + (codeNum++));
	});
	
	/*找回密码*/
	$(".forgetPassword").bind("click",function(){
		$("#password").parent().slideUp("slow");
		$("#vcode").parent().slideUp("slow");
		$("#login_msg").html("");
		$(".forgetPassword").fadeOut("fast");
		$(".login").animate({height:400},"slow");
		$("#btn_login").fadeOut("slow",function(){
			$("#forgetPasswordButton").fadeIn("slow");
			$("#forgetPasswordButton").css("display","block");
			$(".errorSign").html("");
		});
		
	});
	/* 找回密码ajax请求发送 */
	$("#forgetPasswordButton").bind("click",function(){
		var mail = document.getElementById("account").value;
		var checkok = true;
		var rege_email = /^([a-zA-Z0-9_\.\-])+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		if (mail== '') {
			$("#login_msg").html("请输入注册邮箱");
			setTimeout(function(){
				$("#login_msg").fadeOut("fast",function(){
					$("#login_msg").html("");
					$("#login_msg").fadeIn();
				});
			},1500);
			checkok = false;
		} else if (!rege_email.test(mail)) {
			$("#login_msg").html("注册邮箱不合法");
			setTimeout(function(){
				$("#login_msg").fadeOut("fast",function(){
					$("#login_msg").html("");
					$("#login_msg").fadeIn();
				});
			},1500);
			checkok = false;
		}
		if(!checkok){
			return false;
		}
		else
			$("#login_msg").html("发送中……");
		$.ajax({       
			url : '/index/sendForgetMail.do',
			dataType : "json",
			data : {
				mailTo	:  mail,
			},
			error : function(data) {
				alert("error");
			},
			success : function(data) {
				console.log(data.result);
//				var testS = new RegExp("true");
//				console.log(testS.test(data.result));
				if(data.result){
					console.log("发送成功请注意查收");
					$("#login_msg").html("发送成功请注意查收");
					setTimeout(function(){
						$("#login_msg").fadeOut("fast",function(){
							$("#login_msg").html("");
							$("#login_msg").fadeIn();
						});
					},1500);
				}
				else{
					$("#login_msg").html("该账号不存在");
					setTimeout(function(){
						$("#login_msg").fadeOut("fast",function(){
							$("#login_msg").html("");
							$("#login_msg").fadeIn();
						});
					},1500);
				}
			}
		});	
	});

	function floatMsg(msg) {
		this.next().html(msg);
		this.next().fadeIn(500);
		this.css("opacity","0");
	}
})(jQuery);